home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Michael D. Crawford↵ / Word Services SDK 1.0.5 / Writeswell Jr. Source / ObOspec.c < prev    next >
Text File  |  1992-10-06  |  6KB  |  239 lines

  1. /* ObOspec.c
  2.  * Routines to handle typeObjectSpecifier objects for Word Services
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 24 Dec 91 Mike Crawford
  14.  */
  15. #include <AppleEvents.h>
  16. #include <AEObjects.h>
  17. #include <AEPackObject.h>
  18. #include <AERegistry.h>
  19. #include "AppEvents.h"
  20. #include "ObText.h"
  21. #include "ObWind.h"
  22. #include "ObOSpec.h"
  23. #include "Gripe.h"
  24.  
  25.  
  26. /* Given the the direct object of an event is a window token (or property thereof),
  27.  * do the requested event.
  28.  */
  29. OSErr DispatchOspec( AEDesc *tokenPtr,
  30.                         AppleEvent *theAppleEventPtr,
  31.                         AppleEvent *replyEventPtr,
  32.                         long refCon )
  33. {
  34.     OSErr            err;
  35.     AEEventClass    theClass;
  36.     AEEventID        theID;
  37.     
  38.     /* This function is only for the Core suit.  Get the event ID from the appleEvent
  39.      */
  40.     
  41.     err = GetEventID( theAppleEventPtr, &theID );
  42.     
  43.     switch ( theID ){
  44.         case kAEGetData:
  45.             err = OspecGetDataHandler( tokenPtr, theAppleEventPtr, replyEventPtr, refCon );
  46.             break;
  47.         default:
  48.             err = errAEEventNotHandled;
  49.             break;
  50.     }
  51.     
  52.     return noErr;
  53. }
  54.  
  55. /* The handlers for the various events */
  56.  
  57. /* Get Data */
  58.  
  59. OSErr OspecGetDataHandler( AEDesc *tokenPtr,
  60.                         AppleEvent *theAppleEventPtr,
  61.                         AppleEvent *replyEventPtr,
  62.                         long refCon )
  63. {
  64.     DescType        propCode;
  65.     OSpecTokenBody    **tokHdl;
  66.     AEDesc            replyValue;
  67.     Str255            tmpStr;
  68.     OSErr            err;
  69.  
  70.     /* Sanity check */
  71.     if ( tokenPtr->descriptorType != typeOSpecToken ){
  72.         Gripe( "\pGot wrong token type" );
  73.         return errAEEventNotHandled;
  74.     }
  75.  
  76.     tokHdl = (OSpecTokenBody**)(tokenPtr->dataHandle);
  77.     
  78.     propCode = (*tokHdl)->propertyCode;
  79.     
  80.     switch ( propCode ){
  81.         case typeNull:
  82.             /* This is a magic number for "Not A Property".  I don't know if this
  83.              * is really kosher - gotta ask, but it is a convenience.
  84.              */
  85.             
  86.             err = CreateWindTextSpec( (*tokHdl)->theWindowPtr,
  87.                                     (*tokHdl)->textItem,
  88.                                     &replyValue );
  89.             
  90.             if ( err ){
  91.                 Gripe( "\pCreateTextSpecifier failed" );
  92.                 return err;
  93.             }
  94.             
  95.             break;
  96.         case pName:
  97.         case pBounds:
  98.         case pClass:
  99.         case pHasTitleBar:
  100.         case pIndex:
  101.         case pIsModal:
  102.         case pIsResizable:
  103.         case pIsZoomed:
  104.         case pVisible:
  105.             Gripe( "\pGot a property type we do not yet implement" );
  106.             return errAENoSuchObject;
  107.             break;
  108.         default:
  109.             Gripe( "\pUnknown property type" );
  110.             return errAENoSuchObject;
  111.             break;
  112.     }
  113.     
  114.     /* At this point we have some kind of descriptor to stick in the reply */
  115.     
  116.     err = AEPutParamDesc( replyEventPtr,
  117.                             keyDirectObject,
  118.                             &replyValue );
  119.     if ( err ){
  120.         Gripe( "\pAEPutParamDesc failed" );
  121.         return err;
  122.     }
  123.     
  124.     err = AEDisposeDesc( &replyValue );
  125.     if ( err ){
  126.         Gripe( "\pAEDisposeDesc failed" );
  127.         return err;
  128.     }
  129.  
  130.     return noErr;
  131. }/* WindGetDataHandler */
  132.  
  133. /* Return an Ospec token given a window token */
  134.  
  135. pascal OSErr OspecFromWind(DescType desiredClass,
  136.                             const AEDesc *container,
  137.                             DescType containerClass,
  138.                             DescType form,
  139.                             const AEDesc *selectionData,
  140.                             AEDesc *theToken,
  141.                             long LongInt)
  142. {
  143.     AEDesc            longKeyData;
  144.     long            count;
  145.     OSpecTokenBody    tokData;
  146.     OSErr            err;
  147.  
  148.     /* Check that the container is what we intend.  This should only happen if we
  149.      * installed the token handler incorrectly.
  150.      */
  151.  
  152.     if ( container->descriptorType != cWindow )
  153.         return errAEEventNotHandled;
  154.     
  155.     /* find the text block based on the key form */
  156.     
  157.     switch ( form ){
  158.         case formAbsolutePosition:
  159.             /* Make sure we really have a type long descriptor */
  160.             err = AECoerceDesc( selectionData, typeLongInteger, &longKeyData );
  161.             if ( err ){
  162.                 Gripe( "\pAECoerceDesc failed" );
  163.                 return err;
  164.             }
  165.             
  166.             count = **(long**)(longKeyData.dataHandle);
  167.             
  168.             /* We're done with the descriptor created in the coercion */
  169.             
  170.             err = AEDisposeDesc( &longKeyData );
  171.             if ( err ){
  172.                 Gripe( "\pAEDisposeDesc failed" );
  173.                 return err;
  174.             }
  175.             
  176.             /* In our particular case, we can have only one text field in our table.
  177.              * A structured document would have more, so it would need a way to locate
  178.              * the item in the table right here.
  179.              *
  180.              * It is OK to return an error - it is legitimate for the speller to
  181.              * find out how many elements we have by just asking for them until we
  182.              * run out.
  183.              */
  184.  
  185. #ifndef HACK_OSPECS             
  186.             if ( count != 1L ){
  187.                 return errAENoSuchObject;
  188.             }
  189. #else
  190.             if ( count != 1L && count != 2L ){
  191.                 return errAENoSuchObject;
  192.             }
  193. #endif
  194.             
  195.             /* Actually create the token that we return */
  196.  
  197.             tokData.theWindowPtr = ((WindTokenBody*)(*(container->dataHandle)))->theWindowPtr;
  198.             tokData.textItem = count;
  199.             tokData.propertyCode = typeNull;        /* This means it's not a property */
  200.                         
  201.             err = AECreateDesc( typeOSpecToken, (Ptr)&tokData, sizeof( tokData ), theToken );
  202.             if ( err ){
  203.                 Gripe( "\pAECreateDesc failed to create a token" );
  204.                 return err;
  205.             }
  206.             
  207.             return noErr;
  208.             break;
  209.         case formRelativePosition:
  210.         case formTest:
  211.         case formRange:
  212.         case formPropertyID:
  213.         case formName:
  214.             return errAEEventNotHandled;    /* Flesh this out later */
  215.             break;
  216.         default:
  217.             Gripe( "\pGot unexpected key form" );
  218.             return errAEEventNotHandled;
  219.     }
  220.         
  221.     return noErr;
  222. }
  223.  
  224. /*
  225.  * Object counting routines.
  226.  */
  227.  
  228. OSErr CountOSpecInWind( WindowPtr wp, long *countPtr )
  229. {
  230.     /* For us, there can only be one object specifier in a window.  If you have
  231.      * a structured document such as a spreadsheet, you should count the cells that
  232.      * are selected for spellchecking.
  233.      */
  234.     
  235.     *countPtr = 1L;
  236.  
  237.     return noErr;
  238. }
  239.